package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketCollect;
import com.cskaoyan.bean.MarketCollectExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.collect.CollectListVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhang
 * @Date: 2022/11/22 23:10
 * @Since: jdk1.8
 */
@Service
public class WxCollectServiceImpl implements WxCollectService{

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public void addOrDelete(Integer valueId) {
        Subject subject = SecurityUtils.getSubject();
        MarketCollect marketCollect = new MarketCollect();
        marketCollect.setValueId(valueId);
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser)subject.getPrincipal();
            Integer id = user.getId();
            marketCollect.setUserId(id);
            marketCollect.setUpdateTime(new Date());
        }
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
                criteria.andValueIdEqualTo(marketCollect.getValueId());
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);

        if(marketCollects.size()==1){
            //之前收藏过
            MarketCollect marketCollect1 = marketCollects.get(0);
            Boolean deleted = marketCollect1.getDeleted();
            //(收藏改为取消收藏，取消收藏改为收藏)
            marketCollect1.setDeleted(!deleted);
            marketCollectMapper.updateByPrimaryKeySelective(marketCollect1);
        }
        else {
            //之前没收藏过
            marketCollect.setAddTime(new Date());
            marketCollect.setDeleted(false);
            marketCollectMapper.insertSelective(marketCollect);
        }
    }

    @Override
    public CommonData<CollectListVo> list(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<CollectListVo> collectListVos = new ArrayList<>();
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser)subject.getPrincipal();
             collectListVos = marketCollectMapper.selectColectList(user.getId());
//            MarketUser user = (MarketUser)subject.getPrincipal();
//            Integer id = user.getId();
//            MarketCollectExample example = new MarketCollectExample();
//            MarketCollectExample.Criteria criteria = example.createCriteria();
//            criteria.andUserIdEqualTo(id);
//            List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
//            for (MarketCollect marketCollect : marketCollects) {
//                Integer valueId = marketCollect.getValueId();
//                MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);
//                CollectListVo collectListVo = new CollectListVo();
//            }
        }
        PageInfo pageInfo = new PageInfo(collectListVos);
        CommonData data = CommonData.data(pageInfo);
        data.setList(collectListVos);
        return data;
    }
}
